#include "uart_process.h"
#include "my_define.h"
#include "can_process.h"
#include "flash_rw.h"
/*
获取实时状态信息，一次请求只返回一次数据
*/
void GetState()
{
#ifdef DEBUG_USART
    printf("return data;\n");
#endif

    uint8_t header[3] = {0x77, 0x7f, 0x00};
    HAL_UART_Transmit(&huart1, header, 3, 10);
    uint8_t cnt = 1;
    for (size_t i = 0; i < MAXFLOOR; i++)
    {
        if (otherfloor[i].onlinecnt < OFFLINE)
        {
            cnt++;
        }
    }
    uint16_t len = 10 + 9 * cnt;
    uint8_t lendata[2];
    lendata[0] = len & 0xff;
    lendata[1] = len >> 8 & 0xff;
    HAL_UART_Transmit(&huart1, lendata, 2, 10);

    HAL_UART_Transmit(&huart1, &cnt, 1, 10); //这个凑数用的
    HAL_UART_Transmit(&huart1, &cnt, 1, 10);

    uint8_t info[9];
    FLOOR_INFO *f = &thisfloor;
    info[0] = f->id;

    info[1] = f->state.Button | f->state.InfraredDetection << 1;

    info[2] = f->state.AmbientLightLevel & 0xff;
    info[3] = f->state.AmbientLightLevel >> 8 & 0xff;

    info[4] = f->state.LightOutLevel & 0xff;
    info[5] = f->state.LightOutLevel >> 8 & 0xff;

    info[6] = f->state.cnt & 0xff;
    info[7] = f->state.cnt >> 8 & 0xff;

    info[8] = f->state.ControlState;

    HAL_UART_Transmit(&huart1, info, 9, 10);

    for (size_t i = 0; i < MAXFLOOR; i++)
    {
        if (otherfloor[i].onlinecnt < OFFLINE)
        {

            OTHER_STATE *f = &otherfloor[i];
            info[0] = i;

            info[1] = f->state.Button | f->state.InfraredDetection << 1;

            info[2] = f->state.AmbientLightLevel & 0xff;
            info[3] = f->state.AmbientLightLevel >> 8 & 0xff;

            info[4] = f->state.LightOutLevel & 0xff;
            info[5] = f->state.LightOutLevel >> 8 & 0xff;

            info[6] = f->state.cnt & 0xff;
            info[7] = f->state.cnt >> 8 & 0xff;

            info[8] = f->state.ControlState;

            HAL_UART_Transmit(&huart1, info, 9, 10);
        }
    }

    uint8_t ender[3] = {0, 0x7f, 0xf7};
    HAL_UART_Transmit(&huart1, ender, 3, 10);
}

void SendRef(uint8_t id, uint8_t m)
{
    uint8_t header[4] = {0x77, 0x7f, 0x01, m};
    uint8_t ender[2] = {0x7f, 0xf7};
    if (id == thisfloor.id)
    {

        HAL_UART_Transmit(&huart1, header, 4, 10);
        uint8_t info[5];
        FLOOR_INFO *f = &thisfloor;
        info[0] = thisfloor.id;

        info[1] = f->ref.lowlight & 0xff;
        info[2] = f->ref.lowlight >> 8 & 0xff;

        info[3] = f->ref.timeout & 0xff;
        info[4] = f->ref.timeout >> 8 & 0xff;
        HAL_UART_Transmit(&huart1, info, 5, 10);
        HAL_UART_Transmit(&huart1, ender, 2, 10);
    }
    else
    {
        if (id < MAXFLOOR)
        {
            OTHER_STATE *f = &otherfloor[id];
            if (f->onlinecnt < OFFLINE)
            {
                FLOOR_REF ref;
                if (CAN_GetRef(id, &ref))
                {
                    HAL_UART_Transmit(&huart1, header, 4, 10);
                    uint8_t info[5];

                    info[0] = id;

                    info[1] = ref.lowlight & 0xff;
                    info[2] = ref.lowlight >> 8 & 0xff;

                    info[3] = ref.timeout & 0xff;
                    info[4] = ref.timeout >> 8 & 0xff;
                    HAL_UART_Transmit(&huart1, info, 5, 10);
                    HAL_UART_Transmit(&huart1, ender, 2, 10);
                }
            }
        }
    }
}

void SetRef(uint8_t *info)
{
    uint8_t id = info[0];
    if (id == thisfloor.id)
    {
        thisfloor.ref.lowlight = *(uint16_t *)&info[1];
        thisfloor.ref.timeout = *(uint16_t *)&info[3];
        FLASH_FloorInfoSave(thisfloor.id, thisfloor.ref.lowlight, thisfloor.ref.timeout);
    }
    else
    {
        if (id < MAXFLOOR)
        {
            OTHER_STATE *f = &otherfloor[id];
            if (f->onlinecnt < 50)
            {
                FLOOR_REF ref;
                ref.lowlight = *(uint16_t *)&info[1];
                ref.timeout = *(uint16_t *)&info[3];
                CAN_SetRef(id, ref);
            }
        }
    }
}

void Setid(uint8_t *info, uint8_t l)
{
    uint8_t id = info[0];

    if (id == thisfloor.id)
    {
        ChangeID(info[1]);
    }
    else
    {
        if (id < MAXFLOOR)
        {
            OTHER_STATE *f = &otherfloor[id];
            if (f->onlinecnt < OFFLINE)
            {
                CAN_Setid(id, info[1]);
            }
        }
    }
}

void ControlLight(uint8_t id, uint16_t level)
{
    if (id == thisfloor.id)
    {
        thisfloor.state.ControlCnt = 0;
        thisfloor.state.ControlLevel = level;
    }
    else
    {
        if (id < MAXFLOOR)
        {
            OTHER_STATE *f = &otherfloor[id];
            if (f->onlinecnt < OFFLINE)
            {
                CAN_ControlOther(id, level);
            }
        }
    }
}

URAT_LOOP_DATA ss;
/*
串口协议处理函数
*/
void Usartprossion()
{
    //处理设置串口数据
    while (UART_GET_LOOP_DATA(&ss))
    {

#ifdef DEBUG_USART
        printf("len=%d>>", ss.length);
        for (size_t i = 0; i < ss.length; i++)
        {
            printf("%X ", ss.data[i]);
        }
        printf("\n");
#endif

        //解析协议
        switch (ss.data[0])
        {
        case 0x00: //状态
        {
            GetState();
        }
        break;
        case 0x01: //参数
        {
            if (ss.data[1] == 0x00) //读
            {
                SendRef(ss.data[2], 0);
            }
            if (ss.data[1] == 0x01) //写
            {
                SetRef(&ss.data[2]);
                SendRef(ss.data[2], 1);
            }
        }
        break;
        case 0x02: //设置newid
        {
            Setid(&ss.data[1], ss.length);
        }
        break;
        case 0x03: //直接控制灯光亮度
        {
            // uint16_t level = *(uint16_t *)&ss.data[2];
            ControlLight(ss.data[1], *(uint16_t *)&ss.data[2]);
        }
        break;
        case 0x04: //蓝牙控制协议部分********************************************
        {
            BlueTooth(&ss.data[1]);
        }
        break;

        default:
            break;
        }
    }
}
